package com.chapter1;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.learnjava.bean.Info;

import java.util.*;

/**
 * alibaba的json的测试demo
 */
public class AlibabaJSONTest {

    public static void addValue(Map<String, Object>  map){
        for (int i = 0; i < 5; i++) {
            map.put(""+i, i);
        }
    }

    public static void addInfoValue(Info info){
        info.setId(1111);
        info.setUsername("username");
        info.setPassword("密码");
    }

    public static void main(String[] args) {
//        Info info = new Info();
//        addInfoValue(info);
//
//        System.out.println(info.getId());
//        System.out.println(info.getUsername());
//        System.out.println(info.getPassword());

//        Map<String, Object> a = new HashMap<>();
//        a.put("xxx","xxxx");
//
//        addValue(a);
//
//        Iterator i = a.keySet().iterator();
//        while(i.hasNext()){
//            System.out.println(a.get(i.next().toString()));
//        }
//        System.out.println("-------------");

//        for (int i = 0; i < 5; i++) {
//            if(i==2){
//                break;
//            }
//            System.out.println(i);
//        }
//        System.out.println("-------------");

        A a = new A();
        Hand hand = new Hand();
        hand.setHand1("测试Hand-1");
        hand.setHand2(1);
        a.setHand(hand);

        Body body = new Body();
        body.setBody1("ceshi_body_1");
        body.setBody2("ceshi_body_2");
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("uat");
        list.add("pp");
        list.add("4");
        body.setList(list);
        String[] array = new String[]{"1","3","x","m"};
        body.setArray(array);
        a.setBody(body);

        System.out.println("--------------");
        System.out.println(a);
        System.out.println("--------------");

//        String jsonStr = JSON.toJSONString(a);
        String jsonStr = JSONObject.toJSONString(a);
        System.out.println(jsonStr);

        jsonStr = "{\"Body\":{\"List\":[\"1\",\"3\",\"x\",\"m\"],\"Body_1\":\"ceshi_body_1\",\"Body_2\":\"ceshi_body_2\",\"Array\":[\"1\",\"uat\",\"pp\",\"4\"]},\"Hand\":{\"Hand_1\":\"测试Hand-1\",\"Hand_2\":1}}\n";
        System.out.println(jsonStr);
        System.out.println("--------------");
//        A jsonA = JSON.parseObject(jsonStr, A.class);
        A jsonA = JSONObject.parseObject(jsonStr, A.class);
        System.out.println(jsonA);


//        System.out.println(JSONObject.fromObject(a).toString());
    }

    static class A {
        @JSONField(name = "Hand")
        private Hand hand;
        @JSONField(name = "Body")
        private Body body;

        public Hand getHand() {
            return hand;
        }

        public void setHand(Hand hand) {
            this.hand = hand;
        }

        public Body getBody() {
            return body;
        }

        public void setBody(Body body) {
            this.body = body;
        }

        @Override
        public String toString() {
            return "A{" +
                    "hand=" + hand +
                    ", body=" + body +
                    '}';
        }
    }

    static class Hand {

        @JSONField(name = "Hand_1")
        private String hand1;
        @JSONField(name = "Hand_2")
        private Integer hand2;

        public String getHand1() {
            return hand1;
        }

        public void setHand1(String hand1) {
            this.hand1 = hand1;
        }

        public Integer getHand2() {
            return hand2;
        }

        public void setHand2(Integer hand2) {
            this.hand2 = hand2;
        }

        @Override
        public String toString() {
            return "Hand{" +
                    "hand1='" + hand1 + '\'' +
                    ", hand2='" + hand2 + '\'' +
                    '}';
        }
    }

    static class Body {
        @JSONField(name = "Body_1")
        private String body1;
        @JSONField(name = "Body_2")
        private String body2;

        @JSONField(name="List")
        private List<String> list;
        @JSONField(name="Array")
        private String[] array;

        public String getBody1() {
            return body1;
        }

        public void setBody1(String body1) {
            this.body1 = body1;
        }

        public String getBody2() {
            return body2;
        }

        public void setBody2(String body2) {
            this.body2 = body2;
        }

        public List<String> getList() {
            return list;
        }

        public void setList(List<String> list) {
            this.list = list;
        }

        public String[] getArray() {
            return array;
        }

        public void setArray(String[] array) {
            this.array = array;
        }

        @Override
        public String toString() {
            return "Body{" +
                    "body1='" + body1 + '\'' +
                    ", body2='" + body2 + '\'' +
                    ", list=" + list +
                    ", array=" + Arrays.toString(array) +
                    '}';
        }
    }
}

